package web.mails;

import domain.entities.Involucrado;
import domain.entities.MiembroBms;
import domain.entities.SafetyRecipient;
import org.apache.struts2.ServletActionContext;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.*;
import javax.mail.internet.*;
import javax.servlet.ServletContext;
import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;

/**
 * Created with IntelliJ IDEA.
 * User: vgonzalez
 * Date: 03/09/12
 * Time: 11:28
 * To change this template use File | Settings | File Templates.
 */
public class MailUtils extends Authenticator {
    private String from;
    private String pw;
    private static final String ADDRESS_FROM_SAFETY = "mail.probando@yahoo.com";
    private static final String PASS_FROM_SAFETY = "mailcandoit";


    public MailUtils(String from, String password){
        super();
        this.from = from;
        this.pw = password;
    }

    public PasswordAuthentication getPasswordAuthentication(){
        return new PasswordAuthentication(from, pw);
    }

    public void addAttachment(Multipart multipart, File file, String name) throws Exception{
        DataSource source = new FileDataSource(file);
        BodyPart messageBodyPart = new MimeBodyPart();
        messageBodyPart.setDataHandler(new DataHandler(source));
        if(name != null)
            messageBodyPart.setFileName(name);
        multipart.addBodyPart(messageBodyPart);
    }

    public void sendMail(String subject, String body, List<String> toAddressesList, List<String> bccAddressesList,
                         List<File> attachments, List<String> attachmentsNames) throws Exception {
        String host = "smtp.mail.yahoo.com";
       // String password = "mailcandoit";
       // String from = "mail.probando@yahoo.com";
       // String toAddress = "vgonzalez@candoit.com.ar";

        // Get system properties
        Properties props = System.getProperties();
        props.put("mail.smtp.host", host);
        props.put("mail.smtps.auth", "true");
        props.put("mail.smtp.starttls.enable", "true");

        Session session = Session.getInstance(props, this);

        MimeMessage message = new MimeMessage(session);

        message.setFrom(new InternetAddress(from));

            /**Receptores */

        InternetAddress[] toAddresses = new javax.mail.internet.InternetAddress[toAddressesList.size()];

        Iterator<String> itAddr = toAddressesList.iterator();
        int indice = 0;
        while(itAddr.hasNext()){
            toAddresses[indice] = new InternetAddress(itAddr.next());
            indice++;
        }

        if(bccAddressesList != null){
            InternetAddress[] bccAddresses = new javax.mail.internet.InternetAddress[bccAddressesList.size()];
            itAddr = bccAddressesList.iterator();
            indice = 0;
            while(itAddr.hasNext()){
                bccAddresses[indice] = new InternetAddress(itAddr.next());
                indice++;
            }
            message.setRecipients(Message.RecipientType.BCC, bccAddresses);
        }

        message.addHeader("Disposition-Notification-To",from);
        message.setRecipients(Message.RecipientType.TO, toAddresses);


        BodyPart messageBodyPart = new MimeBodyPart();

        Multipart multipart = new MimeMultipart();
        multipart.addBodyPart(messageBodyPart);

        if(attachments != null){
            Iterator<File> it = attachments.iterator();
            Iterator<String> itNames = null;
            if(attachmentsNames != null)
                itNames = attachmentsNames.iterator();
            while(it.hasNext()){
                File actual = it.next();
                if(actual != null){
                    String name = null;
                    if(itNames.hasNext())
                        name = itNames.next();
                    this.addAttachment(multipart, actual, name);
                }
            }
        }
        message.setContent(multipart);
        message.setSubject(subject);

        messageBodyPart.setText(body);

        Transport tr = session.getTransport("smtps");
        tr.connect(host, from, pw);
        tr.sendMessage(message, message.getAllRecipients());
        //     System.out.println("Mail Sent Successfully");
        tr.close();
    }

    public static String getAddressFromSafety(){
        return ADDRESS_FROM_SAFETY;
    }

    public static String getPassFromSafety(){
        return PASS_FROM_SAFETY;
    }

}